﻿using MongoDB.Bson;
using MongoDB.Driver;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DingTalk.Common.Mongodb
{
    public class MongoDBService
    {
        //新增
        public static bool Insert(MongodbHost Host, object t)
        {
            int i = MongodbRepository<object>.Add(Host, t);//成功返回1，失败返回0
            return i > 0;
        }

        /// <summary>
        /// 异步新增
        /// </summary>
        /// <param name="host"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static async Task<bool> AddAsync(MongodbHost host, object t)
        {          
            int i = await MongodbRepository<object>.AddAsync(host, t);
            return i > 0;
        }

        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="host"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool InsertMany(MongodbHost host, List<object> t)
        {
            int i = MongodbRepository<object>.InsertMany(host, t);//成功返回1，失败返回0
            return i > 0;
        }

        /// <summary>
        /// 异步添加一条数据
        /// </summary>
        /// <param name="host"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static async Task<bool> InsertManyAsync(MongodbHost host, List<object> t)
        {
            int i = await MongodbRepository<object>.AddAsync(host, t);
            return i > 0;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="host"></param>
        /// <param name="t"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static UpdateResult Update(MongodbHost host, object t, string id)
        {
            return MongodbRepository<object>.Update(host, t, id);
        }

        /// <summary>
        /// 异步更新
        /// </summary>
        /// <param name="host"></param>
        /// <param name="t"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static async Task<dynamic> UpdateAsync(MongodbHost host, object t, string id)
        {
            return await MongodbRepository<dynamic>.UpdateAsync(host, t, id);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="host"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static dynamic Delete(MongodbHost host, string id)
        {
            return MongodbRepository<dynamic>.Delete(host, id);
        }

        /// <summary>
        /// 异步删除
        /// </summary>
        /// <param name="host"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static async Task<dynamic> DeleteAsync(MongodbHost host, string id)
        {
            return await MongodbRepository<dynamic>.DeleteAsync(host, id);
        }

        //主键查询
        public static string BindById(MongodbHost Host, string id)
        {
            var res = MongodbRepository<object>.FindOne(Host, id);
            string json = JsonConvert.SerializeObject(res);
            return json;
        }

        public static dynamic FindOne(MongodbHost host, string id, string[] field = null)
        {
            return MongodbRepository<dynamic>.FindOne(host, id, field);
        }

        public static async Task<dynamic> FindOneAsync(MongodbHost host, string id, string[] field = null)
        {
            return await MongodbRepository<dynamic>.FindOneAsync(host, id, field);
        }

        public static List<dynamic> FindList(MongodbHost host, FilterDefinition<object> filter, string[] field = null, SortDefinition<object> sort = null)
        {
            return MongodbRepository<dynamic>.FindList(host, filter, field, sort);
        }

        public static List<dynamic> FindListByPage(MongodbHost host, FilterDefinition<object> filter, int pageIndex, int pageSize, long count, string[] field = null, SortDefinition<object> sort = null)
        {
            return MongodbRepository<dynamic>.FindListByPage(host, filter, pageIndex, pageSize, out count, field, sort);
        }

        public static Task<List<dynamic>> FindListAsync(MongodbHost host, FilterDefinition<dynamic> filter, string[] field = null, SortDefinition<dynamic> sort = null)
        {
            return MongodbRepository<dynamic>.FindListAsync(host, filter, field, sort);
        }

        public static Task<List<dynamic>> FindListByPageAsync(MongodbHost host, FilterDefinition<dynamic> filter, int pageIndex, int pageSize, string[] field = null, SortDefinition<dynamic> sort = null)
        {
            return MongodbRepository<dynamic>.FindListByPageAsync(host, filter, pageIndex, pageSize, field, sort);
        }
    }
}
